package cc.rengu.igas.share.core.service.asyncall;

import cc.rengu.igas.momp.facade.response.ComboPaymentReversalResponse;
import cc.rengu.oltp.service.base.DubboAsynBaseService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.dao.PayOrderInfoMapper;
import cc.rengu.oltp.service.common.dao.impl.PayOrderInfoMapperImpl;
import cc.rengu.oltp.service.common.entity.PayOrderInfo;
import cc.rengu.oltp.service.common.enums.BizStatusEnum;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.common.enums.TransStatusEnum;
import cc.rengu.oltp.service.realize.OrderService;
import cc.rengu.oltp.service.realize.impl.OrderServiceImpl;
import cc.rengu.oltp.utility.annotation.BIZ_PROCESS;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 营销合并支付异步冲正
 */
public class AsynComboPaymentRevsalService extends DubboAsynBaseService {

    @BIZ_PROCESS
    public void comboPaymentRevsal() throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String sysNo;
        String instId;
        /*判断记账结果，更新支付订单状态*/
        if (!xmlTreeUtil.isExist(TreeNodeConstant.DUBBO_RESPONSE)) {
            rglog.error("营销冲正dubbo调用失败");
            return;
        }
        rglog.debug("dubbo调用正常返回");
        cc.rengu.igas.acct.facade.result.Result<JSONObject> responseResult = (cc.rengu.igas.acct.facade.result.Result<JSONObject>) xmlTreeUtil.getXmlTreeObjectValue(TreeNodeConstant.DUBBO_RESPONSE, cc.rengu.igas.acct.facade.result.Result.class);
        ComboPaymentReversalResponse response = JSON.toJavaObject(responseResult.getResult(), ComboPaymentReversalResponse.class);

        /*获取交易的系统流水号和机构号*/
        if (response.getHeader() != null && response.getHeader().getTraceNo() != null) {
            sysNo = response.getHeader().getTraceNo().substring(0, 32);
            instId = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID);
        } else {
            rglog.error("营销冲正dubbo调用失败");
            return;
        }

        /*获取支付订单列表*/
        rglog.info("流程日志-获取原营销的支付订单...");
        PayOrderInfoMapper payOrderInfoMapper = new PayOrderInfoMapperImpl();
        List<PayOrderInfo> payOrderInfoList = payOrderInfoMapper.selectPayOrderInfoBySysSeqNum(instId, sysNo);
        if (payOrderInfoList == null || payOrderInfoList.size() == 0) {
            rglog.error("根据机构号<{}>、系统流水号<{}>获取支付订单列表失败", instId, sysNo);
            return;
        }
        List<PayOrderInfo> payOrderInfoListAcct = payOrderInfoList.stream().filter(item -> item.getMsgDstId().equals(ChannelEnum.MOMP.getChannelType())).collect(Collectors.toList());
        if (payOrderInfoListAcct.size() != 1) {
            rglog.error("查询营销支付订单流水记录异常");
            return;
        }
        PayOrderInfo payOrderInfo = payOrderInfoListAcct.get(0);

        /*判断冲正结果，更新状态*/
        rglog.info("流程日志-根据应答码:<{}>处理原营销订单信息...", response.getRespCode());
        if (!OltpRpcdEnum.TRANS_SUCCESS.getRespCode().equals(response.getRespCode())) {
            rglog.error("营销冲正接口调用失败，不更新营销支付订单状态。订单交易状态仍为成功");
        } else {

            rglog.info("营销冲正成功，更新业务状态为已冲正");
            payOrderInfo.setBizStatus(BizStatusEnum.REVSAL.getStatus());
            payOrderInfo.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
            payOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
            OrderService orderService = new OrderServiceImpl();
            if (orderService.updateOrderInfo(null, null, payOrderInfo)) {
                rglog.info("更新营销支付订单信息成功");
            } else {
                rglog.error("更新营销支付订单信息失败");
            }
        }
    }
}
